﻿using JigLibX.Collision;
using Microsoft.Xna.Framework;
using System;
using SolarWinds.Actors;

namespace SolarWinds.Shapes
{
    public class ShipShape : ModelShape
    {
        public int mFrontLeftShieldDamage = 0;
        public int mFrontRightShieldDamage = 0;
        public int mBackLeftShieldDamage = 0;
        public int mBackRightShieldDamage = 0;

        private const float cPiOverFour = (float)Math.PI*0.25f;

        private SphereActor mActor;
        public Actor Actor
        {
            get { return mActor; }
        }

        public ShipShape(Game game)
            : base(game, "ship01")
        {
            BoundingBoxVisible = true;
            BoundingSphereVisible = true;
            
            Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitX, MathHelper.ToRadians(90.0f));
            Scale = Vector3.One * 0.07f;
        }

        protected override void LoadContent()
        {
            base.LoadContent();

            mActor = new SphereActor(this, 0.2f, 0.8f, 0.7f, 100.0f);
            mActor.Skin.callbackFn += SkinCallbackFn;
            mActor.Body.SetBodyInvInertia(0.0f, 0.0f, 0.0f);
        }

        bool SkinCallbackFn(CollisionSkin owner, CollisionSkin collidee)
        {
            var asteroid = collidee.Owner.ExternalData as AsteroidShape;
            if (asteroid != null)
            {
                // collision with asteroid -> reduce shields
                Vector3 dist = asteroid.Position - Position;

                mFrontLeftShieldDamage = 0;
                mFrontRightShieldDamage = 0;
                mBackLeftShieldDamage = 0;
                mBackRightShieldDamage = 0;
                
                if (dist.X < 0) // Right
                {
                    if (dist.Z > 0) // Front
                    {
                        mFrontRightShieldDamage = 1;    
                    }
                    else // Back
                    {
                        mBackRightShieldDamage = 1;
                    }
                }
                else // left
                {
                    if (dist.Z > 0) // Front
                    {
                        mFrontLeftShieldDamage = 1;
                    }
                    else // Back
                    {
                        mBackLeftShieldDamage = 1;
                    }
                }
                

                return false;
            }
            if (collidee.Owner.ExternalData is ShipShape ||
                collidee.Owner.ExternalData is ProjectileShape)
            {
                // collison with ship or projectile
                return false;
            }

            return true;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            Position = mActor.BodyPosition;
            Rotation = mActor.BodyRotation;
        }
    }
}
